package com.EVO.dao;

import com.EVO.entity.Province;

import java.sql.*;

public class ProvinceDao {
    public String getProvinceName(Integer id){
        Connection conn =null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String name = "";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/springdb","root","L4213416");
            ps = conn.prepareStatement("select name from province where id=?");
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                name = rs.getString("name");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(ps != null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(rs != null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return name;

    }
    public Province getProvince(Integer id){
        Connection conn =null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Province province = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/springdb","root","L4213416");
            ps = conn.prepareStatement("select id,name,jiancheng,shenghui from province where id=?");
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                province = new Province(rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("jiancheng"),
                        rs.getString("shenghui"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(ps != null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(rs != null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return province;

    }
}
